package domain.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.google.appengine.api.datastore.Text;

import domain.data.QnAPersistenceManager;



public class GetTrackIp {
	public GetTrackIp(String ip)
	{
		String content = "";
		try {
			PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();

			URL dataURL = new URL("http://whatismyipaddress.com/ip/"+ip);
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(dataURL.openStream(), Charset.forName("iso-8859-1")));
			
			while(1==1)
			{
				String str = reader.readLine();
				if(str==null) break;
				content+=str;
				
			}
			
			Document doc = Jsoup.parse(content);

			@SuppressWarnings("unchecked")
			List<domain.model.TrackIp> ips=  (List<domain.model.TrackIp>)psm.newQuery(domain.model.TrackIp.class,"ip == '"+ip+"'").execute();
			if(ips.size()>0)
			{
				domain.model.TrackIp track = ips.get(0);

				try {
					track.setJsp(doc.select("div[id=section_content]").select("table").get(0).select("td").get(1).text());
				} catch (Exception e) {
					// TODO: handle exception
				}
				try {
					track.setOrganization(doc.select("div[id=section_content]").select("table").get(0).select("td").get(2).text());
				} catch (Exception e) {
					// TODO: handle exception
				}
				try {
					track.setProxy(doc.select("div[id=section_content]").select("table").get(0).select("td").get(3).text());
				} catch (Exception e) {
					// TODO: handle exception
				}
				try {
					track.setType(doc.select("div[id=section_content]").select("table").get(0).select("td").get(4).text());
				} catch (Exception e) {
					// TODO: handle exception
				}
				try {
					track.setAssignment(doc.select("div[id=section_content]").select("table").get(0).select("td").get(5).text());
				} catch (Exception e) {
					// TODO: handle exception
				}
				try {
					track.setCountry(doc.select("div[id=section_content]").select("table").get(1).select("td").get(0).text());
				} catch (Exception e) {
					// TODO: handle exception
				}
				try {
					track.setState(doc.select("div[id=section_content]").select("table").get(1).select("td").get(1).text());
				} catch (Exception e) {
					// TODO: handle exception
				}
				try {
					track.setCity(doc.select("div[id=section_content]").select("table").get(1).select("td").get(2).text());
				} catch (Exception e) {
					// TODO: handle exception
				}

				try {
					dataURL = new URL("http://o2tv.vn/ip.php?ip="+ip);
					
					reader = new BufferedReader(new InputStreamReader(dataURL.openStream(), Charset.forName("iso-8859-1")));
					content = "";
					while(1==1)
					{
						String str = reader.readLine();
						if(str==null) break;
						content+=str;
					}
					doc = Jsoup.parse(content);
					track.setWhoisIp(new Text(doc.select("body").html()));
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				psm=JDOHelper.getPersistenceManager(track);
				psm.currentTransaction().begin();
				psm.makePersistent(track);
				psm.currentTransaction().commit();

			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}

	}
//	public static void main( String args[])
//	{
//		new GetTrackIp("50.17.209.163");
//	}
}
